package org.test.client.ui.component.field;

import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.JTextField;

import org.test.client.ui.util.UIConstants;

public class PlaceHolderTF extends JTextField{

	private String placeHolderStr = null;
	private boolean empty = true;
	
	public PlaceHolderTF(String ph){
		placeHolderStr = ph;
	}
	
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);

		if(this.getText().length() == 0){
			Dimension preferredSize = this.getPreferredSize();
			g.setFont(UIConstants.placeHolderFont);
			g.setColor(UIConstants.placeHolderColor);
			
			FontMetrics fontMetrics = g.getFontMetrics();
			
			int stringWidth = fontMetrics.stringWidth(placeHolderStr);
			int compw = this.getWidth()-10;
			if(compw < stringWidth){
				int tmpw = stringWidth- compw;
				placeHolderStr = placeHolderStr.substring(0, (compw-tmpw/compw)-3)+"...";
			}
			int x = 5;
			
			int comph = this.getHeight();
			int ascent = fontMetrics.getAscent();
			int descent = fontMetrics.getDescent();
			int y = comph / 2 + (ascent - descent) / 2;
			
			g.drawString(placeHolderStr, x, y);
		}
		
	}

	public String getPlaceHolderStr() {
		return placeHolderStr;
	}

	public void setPlaceHolderStr(String placeHolderStr) {
		this.placeHolderStr = placeHolderStr;
	}

	public boolean isEmpty() {
		return empty;
	}

	public void setEmpty(boolean empty) {
		this.empty = empty;
	}
	
}
